﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.Lists.BusinessLogic;
using ApplicationGenerator.Lists.Web;

namespace GUserInterface.WinForms.Lists
{
    public partial class ListActionsControl : BaseWizardPage
    {
        public ListActionsControl()
        {
            InitializeComponent();
            AdditionalInitialize();
        }

        private void AdditionalInitialize()
        {
            ctlAvailableActions.DataSource = ListActionFactory.GetListActionDefinitions();
            ctlAvailableActions.DisplayMember = "Name";
            ctlAvailableActions.SelectedIndex = 0;
        }

        public override void ShowPage(Panel wizardSheet, ListConfiguration listConfiguration)
        {
            if (!wizardSheet.Controls.Contains(this))
            {
                this.Dock = DockStyle.Fill;
                wizardSheet.Controls.Add(this);
            }

            ctlSelectedActions.Items.Clear();
            if (listConfiguration.Actions == null)
            {
                return;
            }
            foreach (var item in listConfiguration.Actions)
            {
                ctlSelectedActions.Items.Add(item);
            }
        }

        public override void SaveConfiguration(ListConfiguration listConfiguration)
        {
            if (listConfiguration.Actions == null)
            {
                listConfiguration.Actions = new List<ListActionBase>();
            }

            listConfiguration.Actions.Clear();

            foreach (var selectedAction in ctlSelectedActions.Items)
            {
                listConfiguration.Actions.Add(selectedAction as ListActionBase);
            }
        }

        public override bool ValidateConfiguration()
        {
            ErrorEntity errors = new ErrorEntity();
            bool isValid = true;
            foreach (var action in ctlSelectedActions.Items)
            {
                if (!(action as ListActionBase).IsValid(errors))
                {
                    isValid = false;
                }
            }

            if (!isValid)
            {
                StringBuilder builder = new StringBuilder();
                foreach (var error in errors.ErrorStrings)
                {
                    builder.AppendLine(error);
                }

                MessageBox.Show(builder.ToString());
                return false;
            }

            return true;
        }

        private void ctlAddActions_Click(object sender, EventArgs e)
        {
            if (ctlAvailableActions.SelectedItem != null)
            {
                ctlSelectedActions.Items.Add(Activator.CreateInstance((ctlAvailableActions.SelectedItem as ActionElement).ActionType));
            }
        }

        private void ctlRemoveAction_Click(object sender, EventArgs e)
        {
            if (ctlSelectedActions.SelectedItem != null)
            {
                ctlSelectedActions.Items.Remove(ctlSelectedActions.SelectedItem);
                ctlSelectedActions.Refresh();
            }
        }

        private void ctlSelectedActions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ctlSelectedActions.SelectedItem != null)
            {
                ctlPropertyGrid.SelectedObject = ctlSelectedActions.SelectedItem;
            }
        }
    }
}
